package net.starwifi.duobao.web;


import net.starwifi.duobao.entity.Ad;
import net.starwifi.duobao.entity.Ad.Type;
import net.starwifi.duobao.mvc.Filter;
import net.starwifi.duobao.mvc.Message;
import net.starwifi.duobao.mvc.Order;
import net.starwifi.duobao.mvc.PageView;
import net.starwifi.duobao.service.AdPositionService;
import net.starwifi.duobao.service.AdService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;


/**
 * Controller - 广告
 * 
 * @author ruoo
 * @version 1.0
 */
@Controller("adminAdController")
@RequestMapping("/ad")
public class AdController extends BaseController {

	@Autowired
	private AdService adService;

	@Autowired
	private AdPositionService adPositionService;



	/**
	 * 添加
	 */
	@RequestMapping(value = "/add", method = RequestMethod.GET)
	public String add(ModelMap model,Long positionId) {
		model.addAttribute("types", Type.values());
		model.addAttribute("positionId",adPositionService.find(positionId));
		model.addAttribute("adPositions", adPositionService.getListData());
		return "/ad/add";
	}

	/**
	 * 保存
	 */
	@RequestMapping(value = "/save", method = RequestMethod.POST)
	public String save(Ad ad, Long adPositionId, RedirectAttributes redirectAttributes) {
		ad.setAdPosition(adPositionService.find(adPositionId));
		if (!isValid(ad)) {
			return ERROR_VIEW;
		}
		if (ad.getBeginDate() != null && ad.getEndDate() != null && ad.getBeginDate().after(ad.getEndDate())) {
			return ERROR_VIEW;
		}
		if (ad.getType() == Type.image) {
            ad.setContent(null);
		}
		adService.save(ad);
		addFlashMessage(redirectAttributes, SUCCESS_MESSAGE);
		return "redirect:/ad/plist?positionId="+adPositionId;
	}

	/**
	 * 编辑
	 */
	@RequestMapping(value = "/edit", method = RequestMethod.GET)
	public String edit(Long id, ModelMap model) {
		model.addAttribute("types", Type.values());
		model.addAttribute("ad", adService.find(id));
		model.addAttribute("adPositions", adPositionService.getListData());
		return "/ad/edit";
	}

	/**
	 * 更新
	 */
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	public String update(Ad ad, Long adPositionId, RedirectAttributes redirectAttributes) {
		ad.setAdPosition(adPositionService.find(adPositionId));
		if (!isValid(ad)) {
			return ERROR_VIEW;
		}
		if (ad.getBeginDate() != null && ad.getEndDate() != null && ad.getBeginDate().after(ad.getEndDate())) {
			return ERROR_VIEW;
		}
		if (ad.getType() == Type.image) {
            ad.setContent(null);
		}
		adService.update(ad);
		addFlashMessage(redirectAttributes, SUCCESS_MESSAGE);
		return "redirect:/ad/plist?positionId="+adPositionId;
	}

	/**
	 * 列表
	 */
	@RequestMapping(value = "/plist", method = RequestMethod.GET)
	public String plist(Long positionId,PageView pageView, ModelMap model,HttpServletRequest request) {
		pageView.filterQuery(request);
		pageView.getOrders().add(Order.desc("id"));
		pageView.getFilters().add(Filter.eq("adPosition.id", positionId));
		model.addAttribute("page", adService.getScrollData(pageView));
		model.addAttribute("positionId",positionId);
		return "/ad/plist";
	}

	/**
	 * 列表
	 */
	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public String list(PageView pageView, ModelMap model) {
		model.addAttribute("page", adService.getScrollData(pageView));
		return "/ad/list";
	}

	/**
	 * 删除
	 */
	@RequestMapping(value = "/delete", method = RequestMethod.POST)
	public @ResponseBody Message delete(Long[] ids) {
		adService.delete(ids);
		return SUCCESS_MESSAGE;
	}


}